package com.sinoeyes.controller.system;

import java.util.List;
import java.util.Set;

import com.sinoeyes.auth.core.LoginUser;
import com.sinoeyes.auth.service.SysLoginService;
import com.sinoeyes.auth.service.SysPermissionService;
import com.sinoeyes.auth.service.TokenService;
import com.sinoeyes.common.result.Result;
import com.sinoeyes.common.utils.ServletUtils;
import com.sinoeyes.system.service.ISysMenuService;
import com.sinoeyes.system.vo.RouterVo;
import com.sinoeyes.system.vo.SysMenuVo;
import com.sinoeyes.system.vo.SysUserVo;
import com.sinoeyes.vo.system.LoginDataVo;
import com.sinoeyes.vo.system.UserInfoVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 登录验证
 */
@RestController
@Api(value = "登录验证 API", tags = {"登录验证"})
public class SysLoginController
{
    @Autowired
    private SysLoginService loginService;

    @Autowired
    private ISysMenuService menuService;

    @Autowired
    private SysPermissionService permissionService;

    @Autowired
    private TokenService tokenService;

    /**
     * 登录方法
     * 
     * @param username 用户名
     * @param password 密码
     * @param code 验证码
     * @param uuid 唯一标识
     * @return 结果
     */
    @PostMapping("/login")
    @ApiOperation("登录方法")
    public Result<LoginDataVo> login(String username, String password, String code, String uuid)
    {
        // 生成令牌
        String token = loginService.login(username, password, code, uuid);
        LoginDataVo loginDataVo = new LoginDataVo();
        loginDataVo.setToken(token);
        return Result.success(loginDataVo);
    }

    /**
     * 获取用户信息
     * 
     * @return 用户信息
     */
    @GetMapping("getInfo")
    @ApiOperation("获取用户信息")
    public Result<UserInfoVo> getInfo()
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        SysUserVo user = loginUser.getUser();
        // 角色集合
        Set<String> roles = permissionService.getRolePermission(user);
        // 权限集合
        Set<String> permissions = permissionService.getMenuPermission(user);
        UserInfoVo userInfoVo = new UserInfoVo();
        userInfoVo.setUserVo(user);
        userInfoVo.setRoles(roles);
        userInfoVo.setPermissions(permissions);
        return Result.success(userInfoVo);
    }

    /**
     * 获取路由信息
     * 
     * @return 路由信息
     */
    @GetMapping("getRouters")
    @ApiOperation("获取路由信息")
    public Result<List<RouterVo>> getRouters()
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        // 用户信息
        SysUserVo user = loginUser.getUser();
        List<SysMenuVo> menus = menuService.selectMenuTreeByUserId(user.getUserId());
        return Result.success(menuService.buildMenus(menus));
    }
}
